package com.jinghao.tws.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.JhHttpClientUtil;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.tws.entity.WvAccessConfig;
import com.jinghao.tws.entity.WvCloudAccessConfig;
import com.jinghao.tws.entity.WvPallet;
import com.jinghao.tws.entity.WvPallet.EWvPallet;
import com.jinghao.tws.entity.WvProcess;
import com.jinghao.tws.entity.WvProcess.EWvProcess;
import com.jinghao.tws.entity.WvProcessConfig;
import com.jinghao.tws.entity.WvProcessMergeConfig;
import com.jinghao.tws.entity.WvProduct;
import com.jinghao.tws.entity.WvProduct.EWvProduct;
import com.jinghao.tws.entity.WvTaskInfo;
import com.jinghao.tws.entity.WvTaskInfo.EWvTaskInfo;
import com.jinghao.tws.entity.WvUploadTaskInfo;
import com.jinghao.tws.entity.WvUploadTaskInfo.EWvUploadTaskInfo;
import com.jinghao.tws.service.IWvAccessConfigService;
import com.jinghao.tws.service.IWvPalletService;
import com.jinghao.tws.service.IWvProcessConfigService;
import com.jinghao.tws.service.IWvProcessMergeConfigService;
import com.jinghao.tws.service.IWvProcessService;
import com.jinghao.tws.service.IWvProductService;
import com.jinghao.tws.service.IWvPublicSynchronousService;
import com.jinghao.tws.service.IWvTaskInfoService;
import com.jinghao.tws.service.IWvUploadTaskInfoService;
import com.jinghao.tws.util.EJhTwsCode;

@Service("wvProductService")
public class WvProductServiceImpl extends JingHaoBaseServiceImpl<WvProduct> implements IWvProductService {
	//==================定制内容开始======================
//在这里修改代码
	@Resource
	private IWvProcessConfigService wvProcessConfigService;
	
	@Resource
	private IWvProcessService wvProcessService;
	
	@Resource
	private IWvUploadTaskInfoService wvUploadTaskInfoService;
	
	@Resource
	private IWvProcessMergeConfigService wvProcessMergeConfigService;
	
	@Resource
	private IWvTaskInfoService wvTaskInfoService;
	
	@Resource
	private IWvPalletService wvPalletService;
	
	@Resource
	private IWvAccessConfigService wvAccessConfigService;
	
	@Resource
	private IWvPublicSynchronousService wvPublicSynchronousService;
	
	
	public void test(){
		this.logger.info("testee");
	}
	
	@Override
	public void updateWvProductUrlLink(WvProduct model) {
		Map<String,Object> map=model.getObjMap();
		if(map==null){
			throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"请求参数有误 ！");
		}
		if(map.get("productUuid")==null||StringUtils.isEmpty(map.get("productUuid").toString())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"请提供产品UUID名称！");
		}
		model.setProductUuid(map.get("productUuid").toString());
		if(map.get("urlLink")==null||StringUtils.isEmpty(map.get("urlLink").toString())){
			throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"请提供理货受损视频地址信息！");
		}
		model.setUrlLink(map.get("urlLink").toString());
		
		JhCnd cnd = new JhCnd();
		cnd.andEquals("productUuid");
		model.lnkCnd(cnd);
		JhUpdate update=new JhUpdate();
		update.add("urlLink").add("urlStatus").add("fileUploadStatus");
		model.setUrlStatus(EWvProduct.urlStatus_1.getKey());
		model.setFileUploadStatus(EWvPallet.fileUploadStatus_2.getKey());
		model.setJhUpdate(update);
		this.updateSame(model);
		
		//保存视频任务生成
		WvUploadTaskInfo wvTaskInfo=new WvUploadTaskInfo();
		wvTaskInfo.setTaskUuid(model.getUUID());
		wvTaskInfo.setCreateTime(new Date());
		wvTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_8.getKey());
		wvTaskInfo.setWorkUuid(model.getProductUuid());
		wvTaskInfo.setTaskLevel(EWvUploadTaskInfo.taskType_8.getKey());
		this.wvUploadTaskInfoService.add(wvTaskInfo);
	}
 
	@Override
	public void listWvProductBySID(WvProduct model) {
		if (StringUtils.isEmpty(model.getSpd())) {
			model.setMapList(null);
			return;
		}
		String flowCode=null;
		//第一步：查询定单相关所有托盘信息
		JhCnd cnd=new JhCnd();
		cnd.append(" and (spd='"+model.getSpd()+"' or order_id = '"+model.getSpd()+"' or fpallet_id = '"+model.getSpd()+"' )");
		model.lnkCnd(cnd);
		model.setPage(null);
		List<Map<String,Object>> list=this.queryMapListById(model, "queryWvProductBySPD");
		if(list==null||list.size()<=0){
			model.setMapList(null);
			return;
		}else{
			flowCode=list.get(0).get("flowCode").toString();
		}
		
		WvProduct newModel=new WvProduct();
		newModel.setSpd(model.getSpd());
		//第二步：查询定单所有相关产品信息
		cnd.reset();
		cnd.append(" and (spd='"+newModel.getSpd()+"' or order_id = '"+newModel.getSpd()+"' or fpallet_id = '"+model.getSpd()+"' )");
		newModel.lnkCnd(cnd);
		newModel.setPage(null);
		List<Map<String,Object>> productlist=this.queryMapListById(newModel, "queryWvProductIdAndTime");
		
		//第三步:拼接数据
		WvProcessConfig wvProModel=new WvProcessConfig();
		WvProcess wvProcess = new WvProcess();
		List<Map<String,Object>> listConfig=null;
		List<Map<String,Object>> listProcess=null;
		String processId=null;
		List<List<Map<String,Object>>> aList=null;
		List<Map<String,Object>> bList=null;
		List<Map<String,Object>> bbList=null;
		Map<String,Object> dataMap=null;
		Map<String,Object> totalDataMap=null;
		String mediaInfo="";
		Date fromDate,toDate,tempDate;
		List<Map<String,Object>> proList=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		
		//获取合并配置信息
		WvProcessMergeConfig wvProcessMergeConfig=new WvProcessMergeConfig();
		cnd.reset();
		cnd.andEquals("flowCode");
		wvProcessMergeConfig.setFlowCode(flowCode);
		wvProcessMergeConfig.setJhCnd(cnd);
		List<String> strList=new ArrayList<String>();
		List<WvProcessMergeConfig> listWvProcessMergeConfig=this.wvProcessMergeConfigService.queryModelList(wvProcessMergeConfig);
		if(listWvProcessMergeConfig.isEmpty()){
			throw new JingHaoCustomerException(EJhTwsCode.TWS10002.getCode(),"请配置环节合并信息！");
		}
		for(WvProcessMergeConfig tempModel:listWvProcessMergeConfig){
			strList.add(tempModel.getProcessCode());
		}
		int _state=1;
		Set<String> dateSet=new HashSet<String>();
		for(Map<String,Object> tempMap:list){
			if(tempMap.get("urlStatus")==null||StringUtils.isEmpty(tempMap.get("urlStatus").toString())){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"托盘记录中不存在urlStatus数据！");
			}
			_state=Integer.parseInt(tempMap.get("urlStatus").toString());

			proList=new ArrayList<Map<String,Object>>();
			dateSet.clear();
			for(Map<String,Object> teMap:productlist){
				if(teMap.get("fpalletId").toString().equals(tempMap.get("fpalletId").toString())){
					if(!dateSet.contains(teMap.get("productId").toString())){
						this.logger.info("---------------"+teMap.get("createTime"));
						try {
							if(teMap.get("createTime").toString().trim().indexOf(".")>0){
								tempDate=(Date)teMap.get("createTime");
							}else{
								tempDate=formatter.parse(teMap.get("createTime").toString().trim());
							}
							teMap.put("createTime", formatter.format(tempDate));
						} catch (ParseException e) {
							throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"createTime时间转化异常！");
						}
						proList.add(teMap);
						dateSet.add(teMap.get("productId").toString());
					}
				}
			}
		    if(proList.size()>0){
		    	tempMap.put("products", proList);
		    }else{
		    	tempMap.put("products", null);
		    }
			//A：根据流程ID，查看流程相关环节编码
			if(tempMap.get("flowCode")==null||StringUtils.isEmpty(tempMap.get("flowCode").toString())){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"托盘记录中不存在flowCode数据！");
			}
			flowCode=tempMap.get("flowCode").toString();
			cnd.reset();
			cnd.andEquals("fcode");
			wvProModel.setFcode(flowCode);
			wvProModel.lnkCnd(cnd);
			wvProModel.setPage(null);
			listConfig=this.wvProcessConfigService.queryMapListById(wvProModel, "queryMapListProcessconfigByProcessId");
			//B：查询流程对应的所有环节数据 fpalletId
			cnd.reset();
			//cnd.andEquals("fpalletId");
			//wvProcess.setFpalletId(tempMap.get("fpalletId").toString());
			cnd.append(" 1=1 and p.fpallet_Id='"+tempMap.get("fpalletId").toString()+"' and pc.fcode='"+flowCode+"'  and pc.process_id<>'tally-event' ");
			wvProcess.lnkCnd(cnd);
			wvProcess.setPage(null);
			listProcess=this.wvProcessService.queryMapListById(wvProcess,"queryWvProcessAndCamera");
			
			aList=new ArrayList<List<Map<String,Object>>>();
			//C:全成数据
			for(Map<String,Object> confMap:listConfig){
				processId=confMap.get("processId").toString();
				bbList=new ArrayList<Map<String,Object>>();
				if(_state==0&&strList.contains(processId)){
					bList=new ArrayList<Map<String,Object>>();
				}
				for(Map<String,Object> proMap:listProcess){
					if(proMap.get("fprocessId")!=null&&proMap.get("fprocessId").toString().equals(processId)){
						tempMap.put(processId+"_fprocessId", processId);
						tempMap.put(processId+"_processUuid", proMap.get("processUuid").toString());
						tempMap.put(processId+"_urlStatus",proMap.get("urlStatus").toString());
						tempMap.put(processId+"_videoStatus",proMap.get("videoStatus")!=null?proMap.get("videoStatus").toString():"");
						tempMap.put(processId+"_progress",proMap.get("progress")!=null?proMap.get("progress").toString():"");
						//未生成视频的，需构建 media-info
						mediaInfo="";
						if(proMap.get("urlStatus")!=null&&Integer.parseInt(proMap.get("urlStatus").toString())==EWvProcess.urlStatus_0.getKey()){
							dataMap=new HashMap<String,Object>();
							dataMap.put("deviceId", Integer.parseInt(proMap.get("deviceId").toString()));
							dataMap.put("channelId", Integer.parseInt(proMap.get("channelId").toString()));
							if(proMap.get("startTime")==null||proMap.get("endTime")==null){
								throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"数据错误，流程环节信息中，startTime或endTime为空");
							}
							fromDate=(Date)proMap.get("startTime");
							toDate=(Date)proMap.get("endTime");
							dataMap.put("from", fromDate.getTime());
							dataMap.put("to", toDate.getTime());
							bbList.add(dataMap);
	
							tempMap.put(processId+"_urlLink","");
						}else{
							if(proMap.get("urlLink")==null||StringUtils.isEmpty(proMap.get("urlLink").toString())){
								throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"数据错误，已生成视频数据，urlLink不能为空");
							}
							String urlLink = proMap.get("urlLink").toString();
							tempMap.put(processId+"_urlLink", urlLink);
						}
						if(_state==0&&strList.contains(processId)){
							fromDate=(Date)proMap.get("startTime");
							toDate=(Date)proMap.get("endTime");
							totalDataMap=new HashMap<String,Object>();
							totalDataMap.put("deviceId", Integer.parseInt(proMap.get("deviceId").toString()));
							totalDataMap.put("channelId", Integer.parseInt(proMap.get("channelId").toString()));
							totalDataMap.put("from", fromDate.getTime());
							totalDataMap.put("to", toDate.getTime());
							bList.add(totalDataMap);
						}
						
					}
				}
				tempMap.put(processId+"_mediaInfo",this.getMediaInfoByList(bbList));
				if(_state==0&&strList.contains(processId)){
					if(bList.size()>0){
						aList.add(bList);
					}
				}
			}
			if(_state==0){
				tempMap.put("media-info", JSON.toJSONString(aList));
			}else{
				tempMap.put("media-info", "");
			}
		}
		model.setMapList(list);
	}
	
	public String getMediaInfo(Map<String,Object> dataMap){
		List<List<Map<String,Object>>> aList=null;
		List<Map<String,Object>> bList=null;
		aList=new ArrayList<List<Map<String,Object>>>();
		bList=new ArrayList<Map<String,Object>>();
		bList.add(dataMap);
		aList.add(bList);
		return JSON.toJSONString(aList);
		
	}
	
	public String getMediaInfoByList(List<Map<String,Object>> bList){
		List<List<Map<String,Object>>> aList=null;
		aList=new ArrayList<List<Map<String,Object>>>();
		aList.add(bList);
		return JSON.toJSONString(aList);
	}

	@Override
	public void listWvProductByPalletId(WvProduct model) {
		if (StringUtils.isEmpty(model.getFpalletId())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"请提供托盘编号！");
		}
		JhCnd cnd=new JhCnd();
		//cnd.andEquals("fpalletId");
		model.setStatus(EWvProduct.status_1.getKey());
		cnd.andEquals("p.status", "status");
		cnd.append(" and p.fpallet_id='"+model.getFpalletId()+"' and pc.process_id <> 'tallying'   ");//老版本代码不需要
		model.lnkCnd(cnd);
		List<Map<String,Object>> list=this.queryMapListById(model, "queryWvProductByPalletId");
		Map<String,Object> dataMap=null;
		Date fromDate=null,toDate=null;
		String mediaInfo="";
		int i=1;
		for(Map<String,Object> map:list){
			map.put("nub", i);
			i++;
			dataMap=new HashMap<String,Object>();
			if(map.get("checkTime")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"checkTime为空！");
			}
			Date checkTime=(Date)map.get("checkTime");
			if(map.get("deviceId")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"deviceId为空！");
			}	
			dataMap.put("deviceId", Integer.parseInt(map.get("deviceId").toString()));
			if(map.get("channelId")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"channelId为空！");
			}	
			dataMap.put("channelId",Integer.parseInt(map.get("channelId").toString()));
			//未生成视频 
			if(map.get("urlStatus")!=null&&Integer.parseInt(map.get("urlStatus").toString())==EWvProduct.urlStatus_0.getKey()){
				fromDate=new Date();
				fromDate.setTime(checkTime.getTime()-2*60*1000);
				dataMap.put("from",fromDate.getTime());
				toDate=new Date();;
				toDate.setTime(checkTime.getTime()+2*60*1000);
				dataMap.put("to",toDate.getTime());
				mediaInfo=this.getMediaInfo(dataMap);
			}
			map.put("media-info", mediaInfo);
		}
		model.setMapList(list);
	}
	public void updateStopGenerateVideo(WvProduct model){
		if(StringUtils.isEmpty(model.getProductUuid())){
			throw new JingHaoCustomerException("提供产品UUID信息！");
		}
		WvTaskInfo wvTask =new WvTaskInfo();
		JhCnd cnd=new JhCnd();
		cnd.reset();
		cnd.andEquals("fproductUuid");
		wvTask.setFproductUuid(model.getProductUuid());
		cnd.andEquals("type");
		wvTask.setType(EWvTaskInfo.type_3.getKey());
		wvTask.setJhCnd(cnd);
		this.wvTaskInfoService.deleteSame(wvTask);
		
		Map<String,Object> dataMap=new HashMap<String,Object>();
		dataMap.put("urlStatus", EWvProduct.urlStatus_0.getKey());
		List<Map<String,Object>> datalist=new ArrayList<Map<String,Object>>();
		datalist.add(dataMap);
		model.setMapList(datalist);
	}
	@Override
	public void updateGenerateVedio(WvProduct model) {
		if(StringUtils.isEmpty(model.getProductUuid())){
			throw new JingHaoCustomerException("提供产品UUID信息！");
		}
		WvProduct wvProduct=this.get(model);
		if(wvProduct==null){
			throw new JingHaoCustomerException("未找到产品信息！");
		}
		JhCnd cnd=new JhCnd();
		WvPallet wvPallet=new WvPallet();
		wvPallet.setPalletId(wvProduct.getFpalletId());
		cnd.andEquals("palletId");
		wvPallet.setJhCnd(cnd);
		List<WvPallet> list=this.wvPalletService.queryModelList(wvPallet);
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException("未找到托盘信息！");
		}
		wvPallet=list.get(0);
		this.synchronousReSetProduct(wvProduct);
		String medioInfo=this.getVedioMediaInfo(wvProduct);
		if(medioInfo!=null&&!medioInfo.trim().equals("")){
			Map<String,Object> map=new HashMap<String,Object>();
			this.updateGenerateVideoTask(wvProduct,medioInfo,wvPallet,map);
			List<Map<String,Object>> datalist=new ArrayList<Map<String,Object>>();
			datalist.add(map);
			model.setMapList(datalist);
		}else{
			throw new JingHaoCustomerException("未找到对应短损信息！");
		}
	}
	public void updateGenerateVedioTemp(WvProduct model) {
		if(StringUtils.isEmpty(model.getProductUuid())){
			throw new JingHaoCustomerException("提供产品UUID信息！");
		}
		WvProduct wvProduct=this.get(model);
		if(wvProduct==null){
			throw new JingHaoCustomerException("未找到产品信息！");
		}
		JhCnd cnd=new JhCnd();
		WvPallet wvPallet=new WvPallet();
		wvPallet.setPalletId(wvProduct.getFpalletId());
		cnd.andEquals("palletId");
		wvPallet.setJhCnd(cnd);
		List<WvPallet> list=this.wvPalletService.queryModelList(wvPallet);
		if(list==null||list.size()<=0){
			throw new JingHaoCustomerException("未找到环节信息！");
		}
		wvPallet=list.get(0);
		//this.synchronousReSetProduct(wvProduct);
		String medioInfo=this.getVedioMediaInfo(wvProduct);
		if(medioInfo!=null&&!medioInfo.trim().equals("")){
			WvTaskInfo wvTask=new WvTaskInfo();
			wvTask.setTaskUuid(model.getUUID());
			wvTask.setFproductUuid(wvProduct.getProductUuid());
			wvTask.setFpalletUuid(wvPallet.getPalletUuid());
			wvTask.setFpalletId(wvPallet.getPalletId());
			wvTask.setCreateTime(new Date());
			wvTask.setMedioInfo(medioInfo);
			wvTask.setVideoStatus(EWvTaskInfo.videoStatus_1.getKey());
			wvTask.setLastCheckTime(new Date());
			wvTask.setType(EWvTaskInfo.type_3.getKey());
			//wvTask.setRequestId(concatenateRequestId);
			this.wvTaskInfoService.add(wvTask);
		}else{
			//throw new JingHaoCustomerException("未找到对应短损信息！");
		}
	}
	
	@Override
	public void updateReSetAllGenerateVedio(WvProduct model) {
		JhCnd cnd=new JhCnd();
		cnd.andEquals("status");
		model.setStatus(EWvProduct.status_1.getKey());
		model.setPage(null);
		model.setJhCnd(cnd);
		List<WvProduct> list=this.queryModelList(model);
		for(WvProduct newModel:list){
			this.updateGenerateVedioTemp(newModel);
		}
		 if(list!=null){
			 list.clear();
			 list=null;
		 }
	}

	public void synchronousReSetProduct(WvProduct wvProduct){
		WvCloudAccessConfig model=new WvCloudAccessConfig();
		//第一步：查询是否配置云段接口及云平台验证
		Map<String,Object> dateInfo=this.wvPublicSynchronousService.cloudLogin(model);
		model=(WvCloudAccessConfig)dateInfo.get("wvCloudAccessConfig");
		JhHttpClientUtil hd = new JhHttpClientUtil();
		Map<String,String> map=new HashMap<String,String>();
		Object obj=dateInfo.get("loginInfo");
		Map<String,Object> loginInfo=(Map<String,Object>)obj;
		map.put("accessTokenData", JSON.toJSONString(loginInfo));
		Map<String,Object> dataMap=null;
		if(wvProduct!=null){
			dataMap=new HashMap<String,Object>();
			dataMap.put("productUuid", wvProduct.getProductUuid());
			dataMap.put("fileName", wvProduct.getFpalletId()+wvProduct.getProductId());
			map.put("jhPublicData", JSON.toJSONString(dataMap));
			String dtUrl="app/ctws/public/syn/updateReSetProductFile";
			String httpUrl=model.getAddress();
			if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
				httpUrl=model.getAddress()+""+dtUrl;
			}else{
				httpUrl=model.getAddress()+"/"+dtUrl; 
			}
			String msg= hd.post(httpUrl, map);
			this.wvPublicSynchronousService.checkResult(msg);
		} 
	}
	public void updateGenerateVideoTask(WvProduct model,String medioInfo,WvPallet wvPallet,Map<String,Object> dataMap){
		WvAccessConfig wvAccessConfig=this.wvAccessConfigService.getWvAccessConfigInfo();
		String userName=wvAccessConfig.getUsername();
		String password=wvAccessConfig.getPassword();
		String dtUrl=wvAccessConfig.getBucket();
		String httpUrl=wvAccessConfig.getAddress();
		if(httpUrl.substring(httpUrl.length()-1, httpUrl.length()).equals("/")){
			httpUrl=wvAccessConfig.getAddress()+""+dtUrl;
		}else{
			httpUrl=wvAccessConfig.getAddress()+"/"+dtUrl;
		}
		int isSuccess=0;
		String url="";
		WvTaskInfo wvTask=new WvTaskInfo();
		//第一步：登录 
		String sessionKey=this.wvAccessConfigService.vmsLogin(userName,password,httpUrl);
		String  concatenateRequestId=null;
		Map<String,Object> videoMap=this.wvAccessConfigService.vmsRequestconcatenatevideo(httpUrl,sessionKey,medioInfo);
		if(videoMap.get("result")!=null&&videoMap.get("result").toString().equals("ok")){
			concatenateRequestId=videoMap.get("concatenate-request-id").toString();
        }else{
        	concatenateRequestId=null;
        }
		
		Map<String,Object> resultMap=null,fileMap=null;
		if(concatenateRequestId!=null&&!concatenateRequestId.trim().equals("")){
			//第三步：获取视频生成状态
			resultMap=this.wvAccessConfigService.vmsCheckconcatenatevideostatus(httpUrl,sessionKey, concatenateRequestId);
			if(resultMap!=null&&resultMap.get("result")!=null&&resultMap.get("result").toString().equals("ok")){
				if(resultMap.get("status")!=null&&resultMap.get("status").toString().equals("COMPLETED")){
					if(resultMap.get("file")!=null){
						fileMap=(Map<String,Object>)resultMap.get("file");
						if(fileMap.get("url")!=null){
							isSuccess=2;
							url=fileMap.get("url").toString();
						}else{
							wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
							wvTask.setErrorInfo(JSON.toJSONString(resultMap));
							//未知错误信息
							this.logger.info("自动生成视频异常信息（checkconcatenatevideostatus）：时间："+new Date()+",result:"+JSON.toJSONString(resultMap));
						}
					}else{
						wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
						wvTask.setErrorInfo(JSON.toJSONString(resultMap));
						//未知错误信息
						this.logger.info("自动生成视频异常信息（checkconcatenatevideostatus）：时间："+new Date()+",result:"+JSON.toJSONString(resultMap));
					}
					
				}else if(resultMap.get("status")!=null&&resultMap.get("status").toString().equals("IN_PROGRESS")){
					if(resultMap.get("progress")!=null){
						isSuccess=1;
						wvTask.setProgress(Integer.parseInt(resultMap.get("progress").toString()));
						wvTask.setVideoStatus(EWvTaskInfo.videoStatus_2.getKey());
					}else{
						wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
						wvTask.setErrorInfo(JSON.toJSONString(resultMap));
						//未知错误信息
						this.logger.info("自动生成视频异常信息（checkconcatenatevideostatus）：时间："+new Date()+",result:"+JSON.toJSONString(resultMap));
					}
				}else{
					wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
					wvTask.setErrorInfo(JSON.toJSONString(resultMap));
					//未知错误信息
					this.logger.info("自动生成视频异常信息（checkconcatenatevideostatus）：时间："+new Date()+",result:"+JSON.toJSONString(resultMap));
				}
			}else if(resultMap!=null&&resultMap.get("result")!=null&&resultMap.get("result").toString().equals("error")){
				wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
				wvTask.setErrorInfo(JSON.toJSONString(resultMap));
			}else{
				wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
				wvTask.setErrorInfo(JSON.toJSONString(resultMap));
				//未知错误信息
				this.logger.info("自动生成视频异常信息（checkconcatenatevideostatus）：时间："+new Date()+",result:"+JSON.toJSONString(resultMap));
			}
		}else{
			wvTask.setVideoStatus(EWvTaskInfo.videoStatus_9.getKey());
			wvTask.setErrorInfo(JSON.toJSONString(videoMap));
        	this.logger.info("自动生成视频异常信息（vmsRequestconcatenatevideo）：时间："+new Date()+",mediaInfo:"+medioInfo+",result:"+JSON.toJSONString(videoMap));
		}
		WvProduct wvProduct=null;
		JhUpdate updateSet=new JhUpdate();
		JhCnd cnd=new JhCnd();
		//第四步：更新任务信息表
		if(isSuccess==0){//生成失败
			throw new JingHaoCustomerException("生成视频发生错误！错误信息："+wvTask.getErrorInfo());
			//throw new JingHaoCustomerException("生成视频发生错误！");
		 //	dataMap.put("urlStatus", EWvProduct.urlStatus_0.getKey());
		 //	dataMap.put("videoStatus", EWvTaskInfo.videoStatus_9.getKey());
		 //	dataMap.put("errorInfo", wvTask.getErrorInfo());
		}else if(isSuccess==1){ //生成中
			
			cnd.reset();
			cnd.andEquals("fproductUuid");
			wvTask.setFproductUuid(model.getProductUuid());
			cnd.andEquals("type");
			wvTask.setType(EWvTaskInfo.type_3.getKey());
			wvTask.setJhCnd(cnd);
			this.wvTaskInfoService.deleteSame(wvTask);
			
			wvTask.setTaskUuid(model.getUUID());
			wvTask.setFproductUuid(model.getProductUuid());
			wvTask.setFpalletUuid(wvPallet.getPalletUuid());
			wvTask.setFpalletId(wvPallet.getPalletId());
			wvTask.setCreateTime(new Date());
			wvTask.setMedioInfo(medioInfo);
			wvTask.setVideoStatus(EWvTaskInfo.videoStatus_2.getKey());
			wvTask.setLastCheckTime(new Date());
			wvTask.setType(EWvTaskInfo.type_3.getKey());
			wvTask.setRequestId(concatenateRequestId);
			this.wvTaskInfoService.add(wvTask);
			
			//环节视频
			cnd.reset();
			updateSet.add("urlStatus").add("fileUploadStatus");
			wvProduct=new WvProduct();
			wvProduct.setProductUuid(model.getProductUuid());
			wvProduct.setUrlStatus(EWvProduct.urlStatus_0.getKey());
			wvProduct.setFileUploadStatus(EWvProduct.fileUploadStatus_0.getKey());
			cnd.andEquals("productUuid");
			wvProduct.setJhCnd(cnd);
			wvProduct.setUpdateSet(updateSet);
			this.updateSame(wvProduct);
			
			dataMap.put("urlStatus", EWvProduct.urlStatus_0.getKey());
			dataMap.put("videoStatus", EWvTaskInfo.videoStatus_2.getKey());
			dataMap.put("progress", wvTask.getProgress());
			
		}else if(isSuccess==2){//生成成功
			
			cnd.reset();
			//环节视频
			updateSet.add("urlStatus").add("urlLink").add("fileUploadStatus");
			wvProduct=new WvProduct();
			wvProduct.setProductUuid(model.getProductUuid());
			wvProduct.setUrlLink(url);
			wvProduct.setUrlStatus(EWvProduct.urlStatus_1.getKey());
			wvProduct.setFileUploadStatus(EWvProduct.fileUploadStatus_1.getKey());
			cnd.andEquals("productUuid");
			wvProduct.setJhCnd(cnd);
			wvProduct.setUpdateSet(updateSet);
			this.updateSame(wvProduct);
			 
			cnd.reset();
			cnd.andEquals("fproductUuid");
			wvTask.setFproductUuid(model.getProductUuid());
			cnd.andEquals("type");
			wvTask.setType(EWvTaskInfo.type_3.getKey());
			wvTask.setJhCnd(cnd);
			this.wvTaskInfoService.deleteSame(wvTask);
			
			dataMap.put("urlStatus", EWvProduct.urlStatus_1.getKey());
			dataMap.put("urlLink", wvProduct.getUrlLink());
		}
		 
	}
	
	public String getVedioMediaInfo(WvProduct model) {
		JhCnd cnd=new JhCnd();
		cnd.andEquals("productUuid");
		model.lnkCnd(cnd);
		model.setStatus(EWvProduct.status_1.getKey());
		cnd.andEquals("p.status", "status");
		cnd.append(" and  pc.process_id <> 'tallying'   ");//老版本代码不需要
		model.setPage(null);
		List<Map<String,Object>> list=this.queryMapListById(model, "queryWvProductByPalletId");
		if(list==null||list.size()<=0){
			//throw new JingHaoCustomerException(EJhTwsCode.TWS10002.getCode(),"未找到短损信息！");
			this.logger.info("productUuid:"+model.getProductUuid());
			return "";
		}
		 
		List<Map<String,Object>> bList=new ArrayList<Map<String,Object>>();
		List<List<Map<String,Object>>> aList=new ArrayList<List<Map<String,Object>>>();
		Date fromDate,toDate;
		Map<String,Object> dataMap=null;
		for(Map<String,Object> map:list){
			dataMap=new HashMap<String,Object>();
			if(map.get("checkTime")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"checkTime为空！");
			}
			if(map.get("deviceId")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"deviceId为空！");
			}	
			dataMap.put("deviceId", Integer.parseInt(map.get("deviceId").toString()));
			if(map.get("channelId")==null){
				throw new JingHaoCustomerException(EJhTwsCode.TWS6001.getCode(),"channelId为空！");
			}	
			dataMap.put("channelId",Integer.parseInt(map.get("channelId").toString()));
			
			Date checkTime=(Date)map.get("checkTime");
			fromDate=new Date();
			fromDate.setTime(checkTime.getTime()-2*60*1000);
			dataMap.put("from",fromDate.getTime());
			toDate=new Date();;
			toDate.setTime(checkTime.getTime()+2*60*1000);
			dataMap.put("to",toDate.getTime());
			bList.add(dataMap);
		}
		aList.add(bList);
		return JSON.toJSONString(aList);
	}
	//==================定制内容结束======================
}
